package com.weasing.res.web.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.weasing.res.domain.WxUserlist;
import com.weasing.res.service.IWxDictService;
import com.weasing.res.service.IWxUserdetailService;
import com.weasing.res.service.IWxUserlistService;
import com.weasing.res.utils.DateUtils;
import com.weasing.res.utils.RESUtils;
import com.weasing.res.utils.StringUtil;
import com.weasing.res.web.action.base.BaseAction;

import net.sf.json.JSONObject;

@Controller
@Scope("prototype")
public class WxUserlistAction extends BaseAction<WxUserlist> {
	@Autowired
	private IWxUserlistService wxUserlistService;
	@Autowired
	private IWxUserdetailService wxUserdetailService;
	@Autowired
	private IWxDictService wxDictService;

	private List<WxUserlist> wxUserlist;
	private String truenamestr;
	private String idcardnostr;
	private WxUserlist wxUser;

	/**
	 * 增加羁押人员基本信息
	 * 
	 * @version 2.0 增加保存省市地区功能
	 * 
	 * @author 黄龙
	 */
	public String add() {
		try {
			if(wxUser.getId()==null||"".equals(wxUser.getId())){
				wxUser.setCreatetime(new Timestamp(System.currentTimeMillis()));
				wxUserlistService.addWxUserlist(wxUser);
			}else{
				wxUser.setCreatetime(new Timestamp(System.currentTimeMillis()));
				wxUserlistService.updateWxUserlist(wxUser);
			}
			return "wxUserlist";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ERROR;
	}

	/**
	 * 查询羁押人员基本信息 左侧条 分页条
	 * 
	 * @version 2.0 加入分页信息
	 * @author 黄龙
	 */
	public String findAll() {
		findDict();
		String currentPageStr = RESUtils.getRequest()
				.getParameter("currentPage");
		if (StringUtil.isNotNulls(currentPageStr)) {
			int currentPage = Integer.valueOf(currentPageStr);
			pageBean.setCurrentPage(currentPage);
		}
		wxUserlistService.pageQuery(pageBean);
		List<WxUserlist> rows = pageBean.getRows();
		wxUserlist = rows;
		RESUtils.getRequest().setAttribute("pageBean", pageBean);
			return "wxUserlist";
	}

	/**
	 * 给模糊查询嫌疑人
	 * 
	 * @author 黄龙
	 * 
	 * 
	 * @return
	 */
	public String list() {
		String currentPageStr = RESUtils.getRequest()
				.getParameter("currentPage");
		if (StringUtil.isNotNulls(currentPageStr)) {
			int currentPage = Integer.valueOf(currentPageStr);
			pageBean.setCurrentPage(currentPage);
		}
		Map<String, Object> map = new HashMap();
		map.put("truename", truenamestr);
		map.put("idcardno", idcardnostr);
		wxUserlistService.pageQuery(pageBean, map);

		List<WxUserlist> rows = pageBean.getRows();
		wxUserlist = rows;
		RESUtils.getRequest().setAttribute("pageBean", pageBean);
		return "wxUserlist";
	}

	/**
	 * 模糊查询转jason
	 * 
	 * @return
	 */
	public String getjsonlist() {
            String curpage = RESUtils.getRequest().getParameter("currentPage");
            if (StringUtil.isNotNulls(curpage)) {
		int currentPage = Integer.valueOf(curpage);
		pageBean.setCurrentPage(currentPage);
            }
            String truename = RESUtils.getRequest().getParameter("truename");
            String idcardno = RESUtils.getRequest().getParameter("idcardno");
		Map<String, Object> map = new HashMap();
            if (StringUtil.isNotNulls(truename)) {
		map.put("truename", truename);
            }
            if (StringUtil.isNotNulls(idcardno)) {
		map.put("idcardno", idcardno);
            }
            if (!map.isEmpty()) {
                wxUserlistService.pageQuery(pageBean, map);
            }else{
                wxUserlistService.pageQuery(pageBean);
            }
            
            List<WxUserlist> rows = pageBean.getRows();
		for (WxUserlist wxUserlist : rows) {
			if (null != wxUserlist.getBirthday()) {
				Date birthday = wxUserlist.getBirthday();
				wxUserlist.setBirthdayStr(DateUtils.date2String(birthday));
			}
			Timestamp createtime = wxUserlist.getCreatetime();
			wxUserlist.setCreatetimeStr(DateUtils.date2String(createtime));
		}
            Map<String,Object> jsonmap = new HashMap<String, Object>();
            jsonmap.put("success", "success");
            jsonmap.put("data",rows);
            
            JSONObject json = JSONObject.fromObject(jsonmap);
            ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
            RESUtils.print(json.toString());
            return null;
        }
        
	public String toEdit(){
		Map<String,Object> map = new HashMap<String, Object>();
		HttpServletRequest request = RESUtils.getRequest();
		String id = request.getParameter("id");
		try {
			wxUser = wxUserlistService.findById(Integer.parseInt(id));
			map.put("success", "success");
			map.put("wxUser", wxUser);
			//生日，转成String传到页面
			map.put("birthday", wxUser.getBirthday().toString());
		} catch (Exception e) {
			map.put("success", "fail");
			e.printStackTrace();
		}
		JSONObject json = JSONObject.fromObject(map);
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		RESUtils.print(json.toString());
		return NONE;
	}
	
	private void findDict(){
		// 查询获取民族
		List<String> nations = StringUtil.String2List(wxDictService
				.findWxDictByDictName("nation"));
		RESUtils.getSession().setAttribute("nations", nations);
		// 查询证件类型
		List<String> credtypes = StringUtil.String2List(wxDictService
				.findWxDictByDictName("credtype"));
		RESUtils.getSession().setAttribute("credtypes", credtypes);
	}

	public List<WxUserlist> getWxUserlist() {
		return wxUserlist;
	}

	public void setWxUserlist(List<WxUserlist> wxUserlist) {
		this.wxUserlist = wxUserlist;
	}

	public String getTruenamestr() {
		return truenamestr;
	}

	public void setTruenamestr(String truenamestr) {
		this.truenamestr = truenamestr;
	}

	public String getIdcardnostr() {
		return idcardnostr;
	}

	public void setIdcardnostr(String idcardnostr) {
		this.idcardnostr = idcardnostr;
	}

	public WxUserlist getWxUser() {
		return wxUser;
	}

	public void setWxUser(WxUserlist wxUser) {
		this.wxUser = wxUser;
	}
	
	/**
	 * 嫌疑人 删除----判断是不是有在评估表里面
	 * 异步请求
	 * @author 黄龙
	 */
	public void deleteUserList() {
		// 0位删除失败或者有关联 1为删除成功
		String parameter = RESUtils.getRequest().getParameter("userid");
		Integer userid = Integer.valueOf(parameter);
		if (wxUserdetailService.findUserdetailByUserId(userid)) {
			RESUtils.print("0");
		} else {
			wxUserlistService.delete(wxUserlistService.findById(userid));
			RESUtils.print("1");
		}
	}
        
        public void getuserlistjson(){
            
        }
       
}
